﻿using LDM.Sys.Repository.Interfaces;
using LDM.Sys.ServiceAPI.Model.DBModel;
using LDM.Sys.ServiceAPI.Model.ViewModel.Request.AuthorityReq;
using LDM.Sys.ServiceAPI.Model.ViewModel.Response.Authority;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Model.Enum.ConfigModel;
using Model.Models;
using Utility.DB.SqlSugar;
using Utility.Extensions;

namespace LDM.Sys.Repository.Repository
{
    /// <summary>
    /// 后台管理用户信息仓储
    /// </summary>
    public class SysAccountRepository : DbContextBase<SysAccount>, ISysAccountRepository, IScoped
    {
        public SysAccountRepository(IConfiguration configuration) :
           base(configuration.GetConnectionString(ConnectionStringType.UserMysql.GetDescription()))
        {
        }
        /// <summary>
        /// 获取后台管理用户信息列表
        /// </summary>
        /// <returns></returns>
        public PageResult<SysAccount> GetSysAccounts(GetSysAccountReq request)
        {
            int totalCount = 0;
            var queryResut = Db.Queryable<SysAccount>()
                .WhereIF(!string.IsNullOrEmpty(request.EnglishAccount), x => x.EnglishAccount.Contains(request.EnglishAccount))
                .WhereIF(!string.IsNullOrEmpty(request.NickName), x => x.NickName.Contains(request.NickName))
                .ToPageList(request.Pagenumber, request.PageSize, ref totalCount);
            return PageResult<SysAccount>.CreateInstance(queryResut, totalCount);
        }


        /// <summary>
        /// 查询后台管理登录
        /// </summary>
        public SysAccount AdminLogin(string userName, string pwd)
        {
            return Db.Queryable<SysAccount>()
              .Where(x => x.EnglishAccount.Equals(userName) && x.Password == pwd)
              .First();
        }


        /// <summary>
        /// 查询所有用户简要信息
        /// </summary>
        public IEnumerable<BriefAccountRes> BriefAccounts()
        {
            return Db.Queryable<SysAccount>().Select(x => new BriefAccountRes
            {
                Id = x.Id,
                AccountName = x.EnglishAccount
            }).ToList();
        }
    }
}
